﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Runtime.CompilerServices;

using ConstructionBidOnline.DAL;
using ConstructionBidOnline.DAL.dsPortfoliosTableAdapters;
using ConstructionBidOnline.Common;

namespace ConstructionBidOnline.Biz
{
    /// <summary>
    /// Portfolios Business Object
    /// </summary>

    public class Portfolios : BOBase
    {
        #region Construction / Disposal

        /// <summary>
        /// Default constructor.
        /// </summary>
        protected Portfolios() { }

        #endregion

        #region Public Properties

        #region Instance Properties


        /// <summary>
        /// PortfolioID.
        /// </summary>
        public int PortfolioID
        {
            get { return _portfolioID; }
            set
            {
                if (_portfolioID != value)
                {
                    if (OnPropertyChanging("PortfolioID", _portfolioID, value))
                    {
                        int oldValue = _portfolioID;
                        _portfolioID = value;
                        OnPropertyChanged("PortfolioID", oldValue, value);
                    }
                }
            }
        }  private int _portfolioID = -1;

        /// <summary>
        /// UserID.
        /// </summary>
        public int UserID
        {
            get { return _userID; }
            set
            {
                if (_userID != value)
                {
                    if (OnPropertyChanging("UserID", _userID, value))
                    {
                        int oldValue = _userID;
                        _userID = value;
                        OnPropertyChanged("UserID", oldValue, value);
                    }
                }
            }
        }  private int _userID = -1;


        /// <summary>
        /// FileType.
        /// </summary>
        public string FileType
        {
            get { return _fileType; }
            set
            {
                if (_fileType != value)
                {
                    if (OnPropertyChanging("FileType", _fileType, value))
                    {
                        string oldValue = _fileType;
                        _fileType = value;
                        OnPropertyChanged("FileType", oldValue, value);
                    }
                }
            }
        }  private string _fileType = null;


        /// <summary>
        /// FileInfo.
        /// </summary>
        public string FileInfo
        {
            get { return _fileInfo; }
            set
            {
                if (_fileInfo != value)
                {
                    if (OnPropertyChanging("FileInfo", _fileInfo, value))
                    {
                        string oldValue = _fileInfo;
                        _fileInfo = value;
                        OnPropertyChanged("FileInfo", oldValue, value);
                    }
                }
            }
        }  private string _fileInfo = null;


        /// <summary>
        /// FileTitle.
        /// </summary>
        public string FileTitle
        {
            get { return _fileTitle; }
            set
            {
                if (_fileTitle != value)
                {
                    if (OnPropertyChanging("FileTitle", _fileTitle, value))
                    {
                        string oldValue = _fileTitle;
                        _fileTitle = value;
                        OnPropertyChanged("FileTitle", oldValue, value);
                    }
                }
            }
        }  private string _fileTitle = null;


        /// <summary>
        /// FileDescription.
        /// </summary>
        public string FileDescription
        {
            get { return _fileDescription; }
            set
            {
                if (_fileDescription != value)
                {
                    if (OnPropertyChanging("FileDescription", _fileDescription, value))
                    {
                        string oldValue = _fileDescription;
                        _fileDescription = value;
                        OnPropertyChanged("FileDescription", oldValue, value);
                    }
                }
            }
        }  private string _fileDescription = null;


        /// <summary>
        /// Date/time at which the Portfolio file was created.
        /// </summary>
        public DateTime? UploadDate
        {
            get { return _uploadDate; }
            set
            {
                if (_uploadDate != value)
                {
                    if (OnPropertyChanging("UploadDate", _uploadDate, value))
                    {
                        DateTime? oldValue = _uploadDate;
                        _uploadDate = value;
                        OnPropertyChanged("UploadDate", oldValue, value);
                    }
                }
            }
        }  private DateTime? _uploadDate = null;

        #endregion

        #endregion

        #region Public Methods

        #region CreateNew

        /// <summary>
        /// Creates a new, empty Portfolios object.
        /// </summary>
        public static Portfolios CreateNew()
        {
            Portfolios obj = new Portfolios();

            // Switch state to Added prior to initializing property values.
            obj.ObjState = ObjectState.Added;

            obj.UploadDate = DateTime.Now;
            return (obj);
        }


        [MethodImpl(MethodImplOptions.Synchronized)]
        public static Portfolios CreateNew(PortfoliosCollection collection)
        {
            Portfolios obj = CreateNew();
            collection.Add(obj);
            return (obj);
        }

        #endregion

        #region GetPortfolioByID

        /// <summary>
        /// Returns an JobCategory object for the specified 'JobCategoryID' or null if no JobCategory with the specified parameter(s) exists.
        /// </summary>
        public static Portfolios GetPortfolioByID(int portfolioid)
        {
            PortfoliosTableAdapter adapter = new PortfoliosTableAdapter();
            adapter.Connection = AppSettings.GetDbConn();

            dsPortfolios.PortfoliosDataTable tbl = adapter.GetData(portfolioid);
            if (tbl.Rows.Count == 1)
            {
                Portfolios p = new Portfolios();
                RelationalToObject(p, tbl.Rows[0]);
                return (p);
            }
            else if (tbl.Rows.Count > 1)
                throw new ApplicationException("Database integrity error: Specified 'PortfolioID' returned more than one record.");

            return (null);
        }

        #endregion

        #region GetCompletePortfoliosByUserID

        /// <summary>
        /// Returns a collection of Portfolios.
        /// </summary>
        public static PortfoliosCollection GetCompletePortfolioByUserID(int userid)
        {
            PortfoliosTableAdapter adapter = new PortfoliosTableAdapter();
            adapter.Connection = AppSettings.GetDbConn();

            dsPortfolios.PortfoliosDataTable tbl = adapter.GetDataByUserID(userid);

            PortfoliosCollection list = new PortfoliosCollection();

            if (tbl.Rows.Count > 0)
            {
                foreach (dsPortfolios.PortfoliosRow row in tbl.Rows)
                {
                    Portfolios portfolios = new Portfolios();
                    RelationalToObject(portfolios, row);
                    list.Add(portfolios);
                }
            }

            return (list);
        }

        #endregion

        #region GetPortfoliosByUserIDnFileType

        /// <summary>
        /// Returns a collection of Portfolios.
        /// </summary>
        public static PortfoliosCollection GetPortfoliosByUserIDnFileType(int userid, string filetype)
        {
            PortfoliosTableAdapter adapter = new PortfoliosTableAdapter();
            adapter.Connection = AppSettings.GetDbConn();

            dsPortfolios.PortfoliosDataTable tbl = adapter.GetDataByUserIDnFileType(userid, filetype);

            PortfoliosCollection list = new PortfoliosCollection();

            if (tbl.Rows.Count > 0)
            {
                foreach (dsPortfolios.PortfoliosRow row in tbl.Rows)
                {
                    Portfolios Portfolios = new Portfolios();
                    RelationalToObject(Portfolios, row);
                    list.Add(Portfolios);
                }
            }

            return (list);
        }

        #endregion

        #region Update

        /// <summary>
        /// Updates the specified Portfolios.
        /// </summary>
        public static void Update(Portfolios obj)
        {
            bool updatePortfolios = false;

            // Do not update invalid objects.
            if (!obj.Validate())
            {
                StringBuilder sb = new StringBuilder(2048);
                sb.Append("The object being updated has one or more invalid properties; the Update() operation for this object has been cancelled.\nError(s):");
                foreach (ValidationError err in obj.Errors)
                    sb.Append("\n* " + err.Element + ": " + err.Message);

                throw new InvalidObjectException(sb.ToString());
            }
            else
            {
                updatePortfolios = (obj.ObjState != ObjectState.Unchanged);

                if (!updatePortfolios)
                    return;
            }

            ObjectState originalObjectState = obj.ObjState;

            dsPortfolios.PortfoliosDataTable tbl = null;
            if (updatePortfolios)
            {
                PortfoliosTableAdapter adapter = new PortfoliosTableAdapter();
                adapter.Connection = AppSettings.GetDbConn();


                tbl = new dsPortfolios.PortfoliosDataTable();
                dsPortfolios.PortfoliosRow row = tbl.NewPortfoliosRow();
                ObjectToRelational(obj, tbl, row);

                adapter.Update(tbl);

                if (originalObjectState == ObjectState.Deleted)
                    obj = null;
                else RelationalToObject(obj, tbl.Rows[0]);
            }
        }

        #endregion

        #region Delete

        /// <summary>
        /// Delete a file.
        /// </summary>
        public static int Delete(int profileid)
        {
            PortfoliosTableAdapter adapter = new PortfoliosTableAdapter();
            adapter.Connection = AppSettings.GetDbConn();
            int d = adapter.Delete(profileid);

            return (d);
        }

        #endregion

        #region Validate

        /// <summary>
        /// Validates the object based on its properties at the time of the call; updates the Errors property.
        /// </summary>
        public override bool Validate()
        {
            Errors.Clear();

            if (ObjState == ObjectState.Unchanged)
            {
                // By definition, unchanged objects are valid.
                return (true);
            }
            else if (ObjState == ObjectState.Uninitialized)
            {
                Errors.Add(new ValidationError("object", "Object is uninitialized.", ValidationError.Severity.Error));

                // No need to proceed past this error.
                return (false);
            }
            else if ((ObjState == ObjectState.Deleted))
            {
                // Deleted items must have 'real' (i.e. non-negative, non-zero) key(s) to be valid.
                Errors.Add(new ValidationError("PortfolioID", "Deleted object has an invalid PKID.", ValidationError.Severity.Error));

                // No need to proceed past this error.
                return (false);
            }
            else  // ObjState == Added | Modified
            {

            }

            return (Errors.Count == 0);
        }

        #endregion

        #region ToString

        /// <summary>
        /// User-friendly string represetation of this object.
        /// </summary>
        public override string ToString()
        {
            return (String.Format("Portfolios: PortfolioID={0}, FileInfo={1}, {2}", PortfolioID, FileInfo));
        }

        #endregion

        #endregion

        #region Privates


        #endregion

    }

    /// <summary>
    /// Collection of 0 or more SubAdmin objects.
    /// </summary>

    public class PortfoliosCollection : List<Portfolios>
    {
        
        #region Types

        public class UserComparer : IComparer<Portfolios>
        {
            public int Compare(Portfolios p1, Portfolios p2)
            {
                return (String.Compare(p1.PortfolioID.ToString(), p2.PortfolioID.ToString()));
            }
        }

        #endregion
        
    }
}
